package com.exam.role.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.exam.role.dao.RoleDao;
import com.exam.role.model.Module;
import com.exam.role.model.Role;
import com.exam.role.model.RoleModule;
import com.exam.role.model.Student;
import com.exam.role.model.Teacher;
import com.exam.role.service.RoleService;
import com.exam.util.Pagetools;

public class RoleServiceImpl implements RoleService {
	private RoleDao roleDao;

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public List<Module> findMenusByPage(Module module, Pagetools page) {
		return roleDao.findMenusByPage(module, page);
	}

	public List<Module> findSubMenus() {
		return roleDao.findSubMenus();
	}

	@Override
	public void save(Object object) {
		roleDao.saveOrUpdate(object);
	}

	@Override
	public Module getMenuById(Integer id) {
		return roleDao.findMenuById(id);
	}

	@Override
	public List<Role> findRolesByPage(Role role, Pagetools page) {
		return roleDao.findRolesByPage(role, page);
	}

	@Override
	public List<Module> getMenuByRole(int roleId) {
		return roleDao.getMenuByRole(roleId);
	}

	@Override
	public List<Student> getStudentByRole(int roleId, Student student,
			Pagetools page) {
		return roleDao.getStudentByRole(roleId, student, page);
	}

	@Override
	public List<Teacher> getTeacherByRole(int roleId, Teacher teacher,
			Pagetools page) {
		return roleDao.getTeacherByRole(roleId, teacher, page);
	}

	@Override
	public List<Module> getMenus(int roleId) {
		List<Module> menus = roleDao.getMenus();
		List<Module> menusInRole = roleDao.getMenuByRole(roleId);
		for (Module module : menus) {
			if (menusInRole.contains(module)) {
				module.setHaveRole(true);
			}
		}
		return menus;
	}

	@Override
	public Role getRole(Integer id) {
		return (Role) roleDao.getById(Role.class, id);
	}

	/**
	 * 保存菜单权限的修改
	 */
	public void saveMenus(String[] parents, String[] sons, Integer id) {
		List<RoleModule> roleModules = roleDao.getRoleModulesByRole(id);
		/**
		 * 删除权限对应菜单
		 */
		roleDao.deleteAll(roleModules);
		Role role = (Role) roleDao.getById(Role.class, id);
		/**
		 * 重新插入新的对应菜单
		 */
		List<RoleModule> newRoleModules = new ArrayList<RoleModule>();
		List<String> mids = new ArrayList<String>(Arrays.asList(parents));
		mids.addAll(Arrays.asList(sons));
		for (String mid : mids) {
			RoleModule roleModule = new RoleModule();
			Module module = (Module) roleDao.getById(Module.class, Integer
					.parseInt(mid));
			roleModule.setModule(module);
			roleModule.setRole(role);
			newRoleModules.add(roleModule);
		}
		if (newRoleModules.size() > 0) {
			roleDao.saveAll(newRoleModules);
		}
	}

	@Override
	public List<Student> findStudentsByPage(int roleId, Student student,
			Pagetools page) {
		return null;
	}

	@Override
	public List<Teacher> findTeachersByPage(int roleId, Teacher teacher,
			Pagetools page) {
		return null;
	}
}
